package org.tony.common.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;

public final class ResourcesHelper
{

  public static final InputStream getResourceAsStream(final String path)
  {
    return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
  }

  public static final URL getResoureURL(final String path)
  {
    return Thread.currentThread().getContextClassLoader().getResource(path);
  }

  public static final URI getResourceURI(final String path)
  {
    try
    {
      return ResourcesHelper.getResoureURL(path).toURI();
    }
    catch (URISyntaxException e)
    {
      return null;
    }
  }

  public static final Properties loadProperties(final String path)
  {
    Properties pro = null;
    InputStream fis = null;
    try
    {
      fis = ResourcesHelper.getResourceAsStream(path);
      if (fis != null)
      {
        pro = new Properties();
        pro.load(fis);
      }
    }
    catch (Exception e)
    {
    }
    finally
    {
      try
      {
        fis.close();
      }
      catch (IOException e)
      {
      }
    }
    return pro;
  }
}
